﻿using Seggu.Dtos;
using Seggu.Services.Interfaces;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Seggu.Desktop.Forms
{
    public partial class ModelosVehiculos : Form
    {
        private IVehicleTypeService vehicleTypeService;
        private IBrandService brandService;
        private IMasterDataService masterDataService;
        private IVehicleModelService vehicleModelService;
        private IBodyworkService bodyworkService;
        private VehicleModelDto currentModel;
        private bool editMode = false;
        private int currentBrandIndex;
        
        public ModelosVehiculos(IBrandService brandService, IVehicleTypeService vehicleTypeService,
            IMasterDataService masterDataService, IVehicleModelService vehicleModelService,
            IBodyworkService bodyworkService)
        {
            InitializeComponent();
            this.brandService = brandService;
            this.vehicleTypeService = vehicleTypeService;
            this.masterDataService = masterDataService;
            this.vehicleModelService = vehicleModelService;
            this.bodyworkService = bodyworkService;
            InitializeComboboxes();
        }

        private void InitializeComboboxes()
        {
            LoadCmbBrands();

            cmbTipoVehiculo.DataSource = this.vehicleTypeService.GetAll().ToList();
            cmbOrigen.DataSource = masterDataService.GetVehicleOrigin().ToList();
        }

        private void cmbMarcas_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (editMode) return;
            currentBrandIndex = cmbMarcas.SelectedIndex;
            FillModelsByBrandId();
        }
            private void LoadCmbBrands()
            {
                cmbMarcas.ValueMember = "Id";
                cmbMarcas.DataSource = this.brandService.GetAll().ToList();
                cmbMarcas.DisplayMember = "Name";

                if (cmbMarcas.Items.Count != 0)
                    FillModelsByBrandId();
                else
                {
                    lstModelos.DataSource = null;
                    cmbOrigen.DataSource = null;
                    cmbTipoVehiculo.DataSource = null;
                }
            }
            private void FillModelsByBrandId()
            {
                if (cmbMarcas.SelectedValue == null) return;
                lstModelos.DataSource = vehicleModelService.GetByBrand(cmbMarcas.SelectedValue.ToString()).ToList();
                lstModelos.ClearSelected();
            }

        private void lstModelos_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lstModelos.Focused) return;
            currentModel = (VehicleModelDto)lstModelos.SelectedItem;
            cmbOrigen.SelectedItem = currentModel.Origin;
            cmbTipoVehiculo.SelectedValue = currentModel.VehicleTypeId;
            txtModelo.Text = currentModel.Name;
            FillBodyworks();
            FillUses();
        }
        private void cmbTipoVehiculo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cmbTipoVehiculo.Focused) return;
            FillBodyworks();
            FillUses();
        }
            private void FillBodyworks()
            {
                lstBodyworks.DataSource = currentModel.Bodyworks;
            }
            private void FillUses()
            {
                lstUses.DataSource = currentModel.Uses;
            }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            SetEditionControls();
        }
            private void SetEditionControls()
            {
                if (!txtUso.Visible)
                {
                    foreach (Control c in Controls)
                    {
                        if (!c.Visible)
                            c.Visible = true;
                    }
                    editMode = true;
                }
                else
                {
                    txtMarcas.Visible = false;
                    txtModelo.Visible = false;
                    txtTipoVehiculo.Visible = false;
                    txtCarroceria.Visible = false;
                    txtUso.Visible = false;
                    btnCarroceria.Visible = false;
                    btnMarcas.Visible = false;
                    btnModelo.Visible = false;
                    btnRemoveModel.Visible = false;
                    btnTipoVehiculo.Visible = false;
                    btnUso.Visible = false;
                    editMode = false;
                }
            }

        #region Txt Select On Click
        private void txtMarcas_Click(object sender, EventArgs e)
        {
            txtMarcas.SelectionStart = 0;
            txtMarcas.SelectionLength =  txtMarcas.Text.Length;
        }

        private void txtTipoVehiculo_Click(object sender, EventArgs e)
        {
            txtTipoVehiculo.SelectionStart = 0;
            txtTipoVehiculo.SelectionLength = txtTipoVehiculo.Text.Length;
        }

        private void txtModelo_Click(object sender, EventArgs e)
        {
            txtModelo.SelectionStart = 0;
            txtModelo.SelectionLength = txtModelo.Text.Length;
        }

        private void txtCarroceria_Click(object sender, EventArgs e)
        {
            txtCarroceria.SelectionStart = 0;
            txtCarroceria.SelectionLength = txtCarroceria.Text.Length;
        }

        private void txtUso_Click(object sender, EventArgs e)
        {
            txtUso.SelectionStart = 0;
            txtUso.SelectionLength = txtUso.Text.Length;
        }
        #endregion

        private void btnModelo_Click(object sender, EventArgs e)
        {
            currentModel.Id = null;
            btnGuardar_Click(sender, e);
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            //try
            {
                var model = this.GetFormInformation();
                vehicleModelService.Save(model);
                ReInitiateForm();
            }
            //catch (Exception)
            //{             
            //    throw;
            //}
        }
            private VehicleModelDto GetFormInformation()
            {
                var model = new VehicleModelDto();
                model.Id = currentModel.Id;
                model.BrandId = cmbMarcas.SelectedValue.ToString();
                model.Origin = cmbOrigen.SelectedItem.ToString();
                model.Bodyworks = currentModel.Bodyworks;
                model.Name = txtModelo.Text;
                model.Uses = currentModel.Uses;
                model.VehicleTypeId = cmbTipoVehiculo.SelectedValue.ToString();
                return model;
            }

        private void btnMarcas_Click(object sender, EventArgs e)
        {
            var brand = new BrandDto();
            brand.Name = this.txtMarcas.Text;
            try
            {
                this.brandService.Save(brand);
                ReInitiateForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
            private void ReInitiateForm()
            {
                MessageBox.Show("Guardó OK!");
                if (editMode)
                    SetEditionControls();
                LoadCmbBrands();
                cmbMarcas.Focus();
                cmbMarcas.SelectedIndex = currentBrandIndex;
                FillModelsByBrandId();
            }   

        private void btnRemoveModel_Click(object sender, EventArgs e)
        {
            string message = "Si este modelo es usado por alguna póliza NO será eliminado\n ¿Continúa?";
            if (MessageBox.Show(message, "Aviso!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    vehicleModelService.Delete(currentModel);
                    ReInitiateForm();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }

        private void cmbMarcas_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cmbMarcas_SelectionChangeCommitted(null, null);
        }

    }
}
